﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using HackApp.DataAccess;
using HackApp.Entities;

namespace HackApp.BusinessLogic
{
    public class IdeaLogic : BusinessLogicBaseClass
    {
      internal IdeaLogic() : base() { }

      public void AddIdea(IdeaDTO ideaDTO)
      {
        Idea newIdea = new Idea();
        unitOfWork.IdeasRepository.Insert(ToDAL(ideaDTO, newIdea));
        unitOfWork.Save();
      }

      /// <summary>
      /// Gets a list of the current ideas
      /// </summary>
      /// <returns></returns>
      public List<IdeaDTO> GetIdeas()
      {
        List<IdeaDTO> ideasDTO = new List<IdeaDTO>();
        IEnumerable<Idea> ideas = unitOfWork.IdeasRepository.Get().OrderByDescending(o=>o.Votes.Count());
        foreach (Idea idea in ideas)
        {
          ideasDTO.Add(ToDTO(idea));
        }
        return ideasDTO;
      }

      /// <summary>
      /// Gets the idea with the specified ideaId
      /// </summary>
      /// <return>Returns the DTO or null if the object is not found</return>
      public IdeaDTO GetById(int id)
      {
        List<IdeaDTO> ideasDTO = new List<IdeaDTO>();
        Idea idea = unitOfWork.IdeasRepository.Get(i=>i.Id == id).FirstOrDefault();
        if (idea != null)
          return ToDTO(idea);
        else return null;
      }

      public bool Vote(int id, MembershipUser user) 
      {
        Idea ideaToVote = this.unitOfWork.IdeasRepository.GetByID(id);
        if (ideaToVote != null)
        {
          aspnet_Users userVoting = this.unitOfWork.UserRepository.GetByID(user.ProviderUserKey);
          if (!ideaToVote.Votes.Contains(userVoting))
          {
            ideaToVote.Votes.Add(userVoting);
            this.unitOfWork.IdeasRepository.Update(ideaToVote);
            this.unitOfWork.Save();
            return true;
          }
        }
        return false;
      }

      public int CountVotesForIdea(int id)
      {
        Idea idea = this.unitOfWork.IdeasRepository.GetByID(id);
        if (idea != null)
        {
          return idea.Votes.Count();
        }
        return -1;
      }

      public PostDTO AddComment(int ideaId, MembershipUser user, PostDTO dto)
      {
        Idea idea = this.unitOfWork.IdeasRepository.GetByID(ideaId);
        Post dmodel = null;
        if (idea != null && user!=null)
        {
          dmodel = this.ToDAL(dto, new Post());
          idea.Posts.Add(dmodel);
          this.unitOfWork.Save();
        }
        return this.ToDTO(dmodel);
      }

      public List<PostDTO> GetComments(int ideaId)
      {
        Idea idea = this.unitOfWork.IdeasRepository.GetByID(ideaId);
        List<PostDTO> ret = new List<PostDTO>();
        for (int i = 0; i < idea.Posts.Count(); i++)
        {
          ret.Add(this.ToDTO(idea.Posts.ElementAt(i)));
        }
        return ret;
      }

      private Post ToDAL(PostDTO dto, Post dmodel)
      {
        dmodel.Id = dto.Id;
        dmodel.Content = dto.Content;
        dmodel.DataCreated = DateTime.Now;
        dmodel.AuthorId = (Guid)Membership.GetUser().ProviderUserKey;
        return dmodel;
      }

      private PostDTO ToDTO(Post dmodel)
      {
        PostDTO dto = new PostDTO();
        dto.Id = dmodel.Id;
        dto.Content = dmodel.Content;
        dto.Author = Membership.GetUser(dmodel.AuthorId).UserName;
        return dto;
      }

      /// <summary>
      /// Maps the idea Data Transfer Object to the Data Model
      /// </summary>
      private Idea ToDAL(IdeaDTO ideaDTO, Idea idea)
      {
        idea.Name = ideaDTO.Name;
        idea.Description = ideaDTO.Description;
        idea.CategoryId = 1;
        idea.DateCreated = DateTime.Now;
        string author = Membership.GetUser().UserName;
        idea.aspnet_Users = unitOfWork.UserRepository.Get(u => u.UserName == author).FirstOrDefault();
        return idea;
      }

      /// <summary>
      /// Maps the idea Data Model to the idea Data Transfer Object
      /// </summary>
      private IdeaDTO ToDTO(Idea ideaDM)
      {
        IdeaDTO ideaDTO = new IdeaDTO();
        ideaDTO.Category = ideaDM.Category.Name;
        ideaDTO.Name = ideaDM.Name;
        ideaDTO.Description = ideaDM.Description;
        ideaDTO.Author = ideaDM.aspnet_Users.UserName;
        ideaDTO.Id = ideaDM.Id;
        return ideaDTO;
      }

    }
}
